﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7Client.ServerManager;
using WP7Client.Helpers;
using MSH.Common.DAO.Results;
using System.Collections.ObjectModel;
using MSH.Common.DAO;
using MSH.Common.Classes;
using WP7Client.Managers;
using GalaSoft.MvvmLight.Command;

namespace WP7Client.Controller
{
    public class SuggestionController : BaseController
    {

        #region Get&Set

        public ICommand AddFriendCommand
        {
            get;
            internal set;
        }

        public ICommand ShowProfileCommand
        {
            get;
            internal set;
        }

        private ObservableCollection<Friend> _suggestions = new ObservableCollection<Friend>();
        public ObservableCollection<Friend> Suggestions
        {
            get { return _suggestions; }
            set
            {
                _suggestions = value;
                OnPropertyChanged("Suggestions");
            }
        }

        #endregion

        public SuggestionController()
        {
            ShowProfileCommand = new RelayCommand<String>(DoShowProfile);
            AddFriendCommand = new RelayCommand<String>(SendFriendInvitation);
        }

        /// <summary>
        /// Nodig een suggestie uit als vriend
        /// </summary>
        /// <param name="username">Gebruikersnaam van de suggestie</param>
        private void SendFriendInvitation(String username)
        {
            Loading = true;
            HttpGet inviteFriendHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "AddFriend/{0}/{1}", UserHelper.Token, username), typeof(AddFriendResult));
            inviteFriendHttpGet.FinishedGet += inviteFriendHttpGet_FinishedGet;
        }

        private void inviteFriendHttpGet_FinishedGet(Object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get != null && (get.resultObject as AddFriendResult) != null)
                {
                    AddFriendResult result = get.resultObject as AddFriendResult;

                    if (result.TokenValidationResult == TokenValidationResultEnum.Valid)
                    {
                        switch (result.Result)
                        {
                            case AddFriendResultEnum.FriendAlreadyRegistered:
                                GlobalHelper.ShowMessage("Error", "Person is already a friend of you or you´ve already received/sent an invitation to this person");
                                break;
                            case AddFriendResultEnum.Succes:
                                GlobalHelper.ShowMessage("", "Invitation sent");
                                break;
                            case AddFriendResultEnum.UserNotExists:
                                break;
                        }
                    }
                    else
                    {

                    }
                }
            }
            catch (Exception ex) { }
            finally { Loading = false; }
        }

        public override void Update()
        {
            base.Update();
            GetSuggestions();
        }

        /// <summary>
        /// Ophalen van vriend suggesties
        /// </summary>
        private void GetSuggestions()
        {
            Loading = true;
            HttpGet SuggestionsHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "GetFriendSuggestions/{0}", UserHelper.Token), typeof(GetFriendSuggestionsResult));
            SuggestionsHttpGet.FinishedGet += new EventHandler(SuggestionsHttpGet_FinishedGet);
        }

        private void SuggestionsHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return;

                GetFriendSuggestionsResult result = ((GetFriendSuggestionsResult)get.resultObject);

                if (result == null)
                    throw new Exception("No result received.");

                if (result.Suggestions == null)
                    throw new Exception("no result list.");

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    Suggestions.Clear();

                    foreach (Friend person in result.Suggestions)
                    {
                        Suggestions.Add(person);
                    }
                    OnPropertyChanged("Suggestions");
                });
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Loading = false;
            }
        }

        /// <summary>
        /// Toon het profiel van de vriend suggestie
        /// </summary>
        /// <param name="username">Gebruikersnaam van de suggestie</param>
        private void DoShowProfile(String username)
        {
            NavigationHelper.NavigateTo(String.Format("/UserInterface/Pages/ProfilePage.xaml?username={0}", username));
        }
    }
}
